
#include <list>


#include <Ice/Ice.h>

#include <asterisk.h>
#include <asterisk/pbx.h>
#include <asterisk/module.h>
#include <asterisk/config.h>


#include <loggerI.hh>
#include <channelI.hh>

#define AST_MODULE "yaami"
static const char* name = "yaami";
static const char* desc = "Yet Another Asterisk Manager Interface";


class Yaami : virtual public Ice::Application {
public:
    virtual int run(int, char*[]) {
    	
    	

		adapter = communicator()->createObjectAdapterWithEndpoints("YAAMI","default -p 6543");

		std::map<std::string,Ice::ObjectPtr>::iterator iter = objects.begin();
		
		for(iter; iter!=objects.end(); iter++)	{
			adapter->add((*iter).second,communicator()->stringToIdentity((*iter).first));
		}
		
		adapter->activate();

        communicator()->waitForShutdown();
        
        if (interrupted())
            ast_log(LOG_ERROR,": terminating");

        return 0;
    }
    
    void addObject(std::string name,Ice::ObjectPtr object)	{
    	
    	objects[name] = object;
    	
    	
	}
    
	
	
	
	
	
private:
	Ice::ObjectAdapterPtr adapter;
    std::map<std::string, Ice::ObjectPtr> objects;
	
	
};

Yaami yaami;




static void* init(void* y)
{
	

	yaami.addObject("logger",new loggerI);
	yaami.addObject("channel",new channelI);

	
	// works in Ice 3.3
	Ice::StringSeq seq(1);
	
	
	
	return (void*)yaami.main(seq);
}



static int unload_module(void)
{


	
	return 0;
}

static int reload(void)
{
	
	
	return 0;
}




static ast_module_load_result  load_module()
{
	
	
	
	pthread_t thread;
	pthread_create(&thread,NULL,init,NULL);
	
	

	
	
	return (ast_module_load_result)0;
}



AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, desc,
		 load_module,
		 unload_module,
		 reload
	       );


